rule variant_recalibrator_snp:
    input:
        norm_vcf="{sample}.norm.vcf.gz",
        ref_fasta=config["ref_fasta"],
        dbsnp_vcf=config["dbsnp"],
        hapmap_snps_vcf=config["hapmap_snps"],
        omni_snps_vcf=config["omni_snps"],
        onekg_snps_vcf=config["onekg_snps"],
    output:
        snp_recal_vcf="{sample}.snp.recal.vcf",
        snp_recal_vcf_idx="{sample}.snp.recal.vcf.idx",
        snp_tranches="{sample}.snp.tranches",
        snp_plots_r="{sample}.snp.plots.R",
    params:
        java_options=config.get("java_options", "")
    shell:
        """
        gatk {params.java_options} VariantRecalibrator \
            -mode SNP \
            -R {input.ref_fasta} \
            -V {input.norm_vcf} \
            -O {output.snp_recal_vcf} \
            --tranches-file {output.snp_tranches} \
            --rscript-file {output.snp_plots_r} \
            -resource:hapmap,known=false,training=true,truth=true,prior=15.0 {input.hapmap_snps_vcf} \
            -resource:omni,known=false,training=true,truth=false,prior=12.0 {input.omni_snps_vcf} \
            -resource:1000G,known=false,training=true,truth=false,prior=10.0 {input.onekg_snps_vcf} \
            -resource:dbsnp,known=true,training=false,truth=false,prior=2.0 {input.dbsnp_vcf} \
            -an DP -an QD -an FS -an SOR -an ReadPosRankSum -an MQRankSum \
            -tranche 100.0 -tranche 99.9 -tranche 99.0 -tranche 95.0 -tranche 90.0 \
            --dont-run-rscript true \
            -OVI true
        """


rule apply_vqsr_snp:
    input:
        norm_vcf="{sample}.norm.vcf.gz",
        ref_fasta=config["ref_fasta"],
        snp_recal_vcf="{sample}.snp.recal.vcf",
        snp_tranches="{sample}.snp.tranches",
    output:
        snp_vqsr_vcf="{sample}.snp.vqsr.vcf.gz",
        snp_vqsr_vcf_tbi="{sample}.snp.vqsr.vcf.gz.tbi",
    params:
        java_options=config.get("java_options", "")
    shell:
        """
        gatk {params.java_options} ApplyVQSR \
            -mode SNP \
            -R {input.ref_fasta} \
            -V {input.norm_vcf} \
            -O {output.snp_vqsr_vcf} \
            --tranches-file {input.snp_tranches} \
            --recal-file {input.snp_recal_vcf} \
            --ts-filter-level 99.0 \
            -OVI true
        """


rule variant_recalibrator_indel:
    input:
        snp_vqsr_vcf="{sample}.snp.vqsr.vcf.gz",
        ref_fasta=config["ref_fasta"],
        mills_indels_vcf=config["mills_indels"],
        known_indels_vcf=config["known_indels"],
    output:
        indel_recal_vcf="{sample}.indel.recal.vcf",
        indel_recal_vcf_idx="{sample}.indel.recal.vcf.idx",
        indel_tranches="{sample}.indel.tranches",
        indel_plots_r="{sample}.indel.plots.R",
    params:
        java_options=config.get("java_options", "")
    shell:
        """
        gatk {params.java_options} VariantRecalibrator \
            -mode INDEL \
            -R {input.ref_fasta} \
            -V {input.snp_vqsr_vcf} \
            -O {output.indel_recal_vcf} \
            --tranches-file {output.indel_tranches} \
            --rscript-file {output.indel_plots_r} \
            -resource:mills,known=true,training=true,truth=true,prior=12.0 {input.mills_indels_vcf} \
            -resource:known_indels,known=true,training=true,truth=true,prior=10.0 {input.known_indels_vcf} \
            -an DP -an QD -an FS -an SOR -an ReadPosRankSum -an MQRankSum \
            --dont-run-rscript true \
            --max-gaussians 6 \
            -OVI true
        """

rule apply_vqsr_indel:
    input:
        snp_vqsr_vcf="{sample}.snp.vqsr.vcf.gz",
        ref_fasta=config["ref_fasta"],
        indel_recal_vcf="{sample}.indel.recal.vcf",
        indel_tranches="{sample}.indel.tranches",
    output:
        snv_vcf="{sample}.snv.vcf.gz",
        snv_vcf_tbi="{sample}.snv.vcf.gz.tbi",
    params:
        java_options=config.get("java_options", "")
    shell:
        """
        gatk {params.java_options} ApplyVQSR \
            -mode INDEL \
            -R {input.ref_fasta} \
            -V {input.snp_vqsr_vcf} \
            -O {output.snv_vcf} \
            --recal-file {input.indel_recal_vcf} \
            --tranches-file {input.indel_tranches} \
            --ts-filter-level 99.0 \
            -OVI true
        """